﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class SanPhamDAO
    {
        #region 1   -   Queries

        public static SanPhamDTO LaySanPhamTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            SAN_PHAM temp = myDB.SAN_PHAMs.Single(c => c.ID == id);
            return ChuyenTuSanPhanSangSanPhamDTO(temp);
        }


        public static List<SanPhamDTO> LaySanPhamTheoTenSanPham(string tensanpham)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            List<SanPhamDTO> result = new List<SanPhamDTO>();
            try
            {
                SAN_PHAM temp = myDB.SAN_PHAMs.Single(c => c.TenSanPham == tensanpham);

                result.Add(ChuyenTuSanPhanSangSanPhamDTO(temp));
                return result;

            }
            catch (Exception)
            {
                return result;
            }

        }

        public static SanPhamDTO LaySanPhamTheoTenSanPham1(string tensanpham)
        {
            try
            {
                MyDataBaseDataContext myDB = new MyDataBaseDataContext();
                SAN_PHAM temp = myDB.SAN_PHAMs.Single(c => c.TenSanPham == tensanpham);
                return ChuyenTuSanPhanSangSanPhamDTO(temp);
            }
            catch (Exception)
            {
                return null;
            }

        }

        public static List<SanPhamDTO> LayDanhSachSanPham()
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listSP = from sp in myDB.SAN_PHAMs
                         select sp;


            List<SanPhamDTO> result = new List<SanPhamDTO>();
            foreach (SAN_PHAM item in listSP)
            {
                result.Add(ChuyenTuSanPhanSangSanPhamDTO(item));
            }
            return result;
        }

        public static List<SanPhamDTO> LayDanhSachSanPhamBoQuaNSanPham(int n)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listSP = (from sp in myDB.SAN_PHAMs
                          select sp).Skip(n);


            List<SanPhamDTO> result = new List<SanPhamDTO>();
            foreach (SAN_PHAM item in listSP)
            {
                result.Add(ChuyenTuSanPhanSangSanPhamDTO(item));
            }
            return result;
        }
        public static List<SanPhamDTO> TraCuuSanPhamCoChuaTenSanPham(string tensanpham, int giatu, int giaden, string tenhangsanxuat)
        {
            HangSanXuatDTO hangsanxuat = HangSanXuatDAO.LayHangSanXuatTheoTenHangSanXuat(tenhangsanxuat);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listSP = from sp in myDB.SAN_PHAMs
                         where sp.TenSanPham.Contains(tensanpham) &&
                         sp.GiaBanHienTai >= giatu && sp.GiaBanHienTai <= giaden &&
                         sp.MaHangSanXuat == hangsanxuat.ID
                         select sp;


            List<SanPhamDTO> result = new List<SanPhamDTO>();
            try
            {
                foreach (SAN_PHAM item in listSP)
                {
                    result.Add(ChuyenTuSanPhanSangSanPhamDTO(item));
                }
            }
            catch (Exception)
            {

            }

            return result;

        }

        public static List<SanPhamDTO> TraCuuSanPhamKhongChuaTenSanPham(int giatu, int giaden, string tenhangsanxuat)
        {
            HangSanXuatDTO hangsanxuat = HangSanXuatDAO.LayHangSanXuatTheoTenHangSanXuat(tenhangsanxuat);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listSP = from sp in myDB.SAN_PHAMs
                         where sp.GiaBanHienTai >= giatu && sp.GiaBanHienTai <= giaden &&
                         sp.MaHangSanXuat == hangsanxuat.ID
                         select sp;


            List<SanPhamDTO> result = new List<SanPhamDTO>();
            try
            {
                foreach (SAN_PHAM item in listSP)
                {
                    result.Add(ChuyenTuSanPhanSangSanPhamDTO(item));
                }
            }
            catch (Exception)
            {

            }

            return result;

        }

        public static List<SanPhamDTO> LayDanhSachSanPhamTheoNhomDanhMucSanPham(int manhomdanhmucsanpham)
        {
            List<DanhMucSanPhamDTO> listdanhmucsp = DanhMucSanPhamDAO.LayDanhSachDanhMucSanPhamTheoMaNhomDanhMucSanPham(manhomdanhmucsanpham);
            List<SanPhamDTO> result = new List<SanPhamDTO>();
            for (int i = 0; i < listdanhmucsp.Count; i++)
            {
                result = result.Union(LayDanhSachSanPhamTheoMaDanhMucSanPham(listdanhmucsp[i].ID)).ToList();
            }
            return result;
        }

        public static List<SanPhamDTO> LayDanhSachSanPhamTheoMaDanhMucSanPham(int madanhmucsanpham)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var ListSP = from sp in myDB.SAN_PHAMs
                         where sp.MaDanhMucSanPham == madanhmucsanpham
                         select sp;

            List<SanPhamDTO> result = new List<SanPhamDTO>();
            foreach (SAN_PHAM item in ListSP)
            {
                SanPhamDTO temp = ChuyenTuSanPhanSangSanPhamDTO(item);
                result.Add(temp);
            }
            return result;
        }

        public static int DemSoLuongSanPham()
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            return myDB.SAN_PHAMs.Count();
        }

        public static List<SanPhamDTO> LayDanhSachSanPhamTheoMaHangSanXuat(int mahangsanxuat)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var ListSP = from sp in myDB.SAN_PHAMs
                         where sp.MaHangSanXuat == mahangsanxuat
                         select sp;

            List<SanPhamDTO> result = new List<SanPhamDTO>();
            foreach (SAN_PHAM item in ListSP)
            {
                SanPhamDTO temp = ChuyenTuSanPhanSangSanPhamDTO(item);
                result.Add(temp);
            }
            return result;
        }

        public static List<SanPhamDTO> LayDanhSachSanPhamTheoDoUuTien(int douutien)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var ListSP = from sp in myDB.SAN_PHAMs
                         where sp.MaDanhMucSanPham == douutien
                         select sp;

            List<SanPhamDTO> result = new List<SanPhamDTO>();
            foreach (SAN_PHAM item in ListSP)
            {
                SanPhamDTO temp = ChuyenTuSanPhanSangSanPhamDTO(item);
                result.Add(temp);
            }
            return result;
        }

        public static bool CapNhatSanPham(SanPhamDTO sanphamDTO)
        {
            try
            {
                MyDataBaseDataContext myDB = new MyDataBaseDataContext();
                SAN_PHAM temp = myDB.SAN_PHAMs.Single(c => c.ID == sanphamDTO.ID);
                temp.TenSanPham = sanphamDTO.TenSanPham;
                temp.HinhSanPham = sanphamDTO.HinhSanPham;
                temp.GiaBanHienTai = sanphamDTO.GiaBanHienTai;
                temp.SoLuotMua = sanphamDTO.SoLuotMua;
                temp.SoLuotXem = sanphamDTO.SoLuotXem;
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
        public static bool ThemSanPham(SanPhamDTO sanphamDTO)
        {
            SAN_PHAM temp = ChuyenTuSanPhanDTOSangSanPham(sanphamDTO);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            try
            {
                myDB.SAN_PHAMs.InsertOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaSanPhamTheoID(int id)
        {
            SanPhamThuongDangBanTrenGianHangDAO.XoaSanPhamTrongGianHangTheoMaSanPham(id);
            ChiTietDienThoaiDAO.XoaChiTietDienThoaiTheoMaSanPham(id);
            ChiTietLapTopDAO.XoaChiTietLapTopTheoMaSanPham(id);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            SAN_PHAM temp = myDB.SAN_PHAMs.Single(c => c.ID == id);
            try
            {
                myDB.SAN_PHAMs.DeleteOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool KiemTraSanPhamLaLapTop(int id)
        {
            SanPhamDTO sanpham = SanPhamDAO.LaySanPhamTheoID(id);
            DanhMucSanPhamDTO danhmucsanpham = DanhMucSanPhamDAO.LayDanhMucSanPhamTheoID(sanpham.MaDanhMucSanPham);
            if (danhmucsanpham.MaNhomDanhMucSanPham == 1)
                return true;
            return false;
        }
        #endregion

        #region 2   -   Function Helper

        public static SanPhamDTO ChuyenTuSanPhanSangSanPhamDTO(SAN_PHAM sanpham)
        {
            SanPhamDTO result = new SanPhamDTO();
            //result.DoUuTien = sanpham.DoUuTien.Value;
            result.GiaBanHienTai = sanpham.GiaBanHienTai.Value;
            result.HinhSanPham = sanpham.HinhSanPham;
            result.ID = sanpham.ID;
            result.MaDanhMucSanPham = sanpham.MaDanhMucSanPham.Value;
            result.MaHangSanXuat = sanpham.MaHangSanXuat.Value;
            result.SoLuotMua = sanpham.SoLuotMua.Value;
            result.SoLuotXem = sanpham.SoLuotXem.Value;
            result.TenSanPham = sanpham.TenSanPham;
            return result;
        }

        public static SAN_PHAM ChuyenTuSanPhanDTOSangSanPham(SanPhamDTO sanphamDTO)
        {
            SAN_PHAM result = new SAN_PHAM();
            result.DoUuTien = sanphamDTO.DoUuTien;
            result.GiaBanHienTai = sanphamDTO.GiaBanHienTai;
            result.HinhSanPham = sanphamDTO.HinhSanPham;
            result.ID = sanphamDTO.ID;
            result.MaDanhMucSanPham = sanphamDTO.MaDanhMucSanPham;
            result.MaHangSanXuat = sanphamDTO.MaHangSanXuat;
            result.SoLuotMua = sanphamDTO.SoLuotMua;
            result.SoLuotXem = sanphamDTO.SoLuotXem;
            result.TenSanPham = sanphamDTO.TenSanPham;
            return result;
        }

        #endregion
    }
}
